<template>
 <div class="index">
   <!-- 头部 -->
     <el-container>
     <el-header>
       <div class="logo">
         <img src="../assets/logo.png" alt=""></div>
       <div class="title">
         <h1>电商后台管理系统</h1>
       </div>
       <div class="logout">
         欢迎光临 ~
         <a href="javascript:;" @click='logout'>退出</a>
       </div>
     </el-header>
      <el-container>
    <!-- 侧边栏 -->
      <el-aside width="200px">
        <!-- default-active 默认高亮项
             unique-opened 是否只保持一个子菜单的展开
             router:给大盒子添加router 使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转
             :default-active当前激活菜单的  要获取对应的index /users /rights
         -->
      <el-menu
      router
      unique-opened
      background-color="#545c64"
      text-color="#fff"
      active-text-color="#ffd04b"
      :default-active="defaultActive"
      >
      <!-- 一级菜单 -->
      <el-submenu  v-for="menu in menuList" :key="menu.id" :index="menu.path">
        <template slot="title">
          <i class="el-icon-location"></i>
          <span>{{menu.authName}}</span>
        </template>
        <el-menu-item-group>
          <!-- 二级菜单  -->
           <el-menu-item  v-for='item in menu.children' :key="item.id" :index="item.path">
          <i class="el-icon-menu"></i>
          <span slot="title">{{item.authName}}</span>
          </el-menu-item>
        </el-menu-item-group>
      </el-submenu>
     </el-menu>
      </el-aside>
       <el-main>
         <router-view></router-view>
       </el-main>
      </el-container>
    </el-container>
 </div>
</template>

<script>
export default {
  created () {
    // 发送axios渲染侧边栏
    this.$axios.get('menus', {
      // 配置请求头确保每一次都能够携带令牌
    }).then(res => {
      // console.log(res)
      const { data, meta } = res
      if (meta.status === 200) {
        this.menuList = data
        // console.log(this.menuList)
      } else {
        this.$message.error(meta.msg)
      }
    })
  },
  data () {
    return {
      menuList: []
    }
  },
  methods: {
    logout () {
      this.$confirm('尊敬的用户! 你确定要退出吗???', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message.success('删除成功')
        localStorage.removeItem('token')
        this.$router.push('/login')
      }).catch(() => {
      })
    }
  },
  computed: {
    defaultActive () {
      // 获取当前的路由 \users ==> index = users
      // console.log(this.$route)
      return this.$route.path.slice(1).split('-')[0]
    }
  }
}
</script>

<style lang='scss' scoped >
.index{
   height: 100%;
  .el-container{
    height: 100%;
  }
  .el-header{
  background-color: #d8d8d8;
  display: flex;
  .logo{
    width: 200px;
    img{
      height: 40px;
      margin: 10px;
    }
  }
  .title{
    flex: 1;
    h1{
      height: 60px;
      line-height: 60px;
      text-align: center;
    }
  }
  .logout{
    width: 180px;
    height: 60px;
    line-height: 60px;
    font-weight: 700;
    text-align: right;
    a{
      color: orange;
    }
  }
  }
  .el-aside{
  background-color: #545c64;
  .el-menu{
    border-right:none;
  }
  }
  .el-main{
  background-color: #ecf0f1;
  }
}
</style>
